<?php
namespace app\api\controller;

use app\admin\model\jianshebaorecord\Jianshebaorecord;
use app\admin\model\jianshebaosend\Jianshebaosend;
use app\admin\model\system\System;
use app\common\controller\Api;
use think\Db;
use think\Exception;

class Jianshebao extends Api{
    protected $noNeedLogin = ['list','sendjianshebao'];
    protected $noNeedRight = ['*'];

    public function list(){
        $jianshebaoModel = new \app\admin\model\jianshebao\Jianshebao();
        $list = collection($jianshebaoModel->where([])->select());
        if($list){
            $lisst = $list->toArray();
            $this->success('成功',$list);
        }else{
            $this->success('成功',[]);
        }
    }

    public function buy(){
        $this->error('建设宝功能正在升级中');
        $jianshebao_id = $this->request->param('jianshebao_id');
        $amount = $this->request->param('amount');
        $pay_password = $this->request->param('pay_password');
        $userinfo = $this->auth->getUserinfo();
        if($amount > $userinfo['shouyi_balance']){
            $this->error('余额不足');
        }
        $userModel = new \app\admin\model\User();
        $userinfo = $userModel->where(['id'=>$userinfo['id']])->find()->toArray();
        if($userinfo['pay_password'] ==''){
            return json_encode([
                'code'=>101,
                'msg'=>'您还未设置交易密码',
                'data'=>[]
            ]);
        }
        $new_password = md5($pay_password);
        if($new_password!=$userinfo['pay_password']){
            $this->error('交易密码不正确');
        }
        $jianshebaoModel = new \app\admin\model\jianshebao\Jianshebao();
        $jianshebaoinfo = $jianshebaoModel->where(['id'=>$jianshebao_id])->find()->toArray();
        if($amount<$jianshebaoinfo['min_price'] || $amount>$jianshebaoinfo['max_price']){
            $this->error('输入金额必须大于等于'.$jianshebaoinfo['min_price'].'小于等于'.$jianshebaoinfo['max_price']);
        }

        $yugu_amount=$amount*$jianshebaoinfo['bili']/100;

        Db::startTrans();
        try{
            //扣掉用户的余额
            $userModel = new \app\admin\model\User();
            $userModel->where(['id'=>$userinfo['id']])->update([
                'shouyi_balance'=>$userinfo['shouyi_balance']-$amount,
            ]);

            //写入投资纪录表
            $recordModel = new Jianshebaorecord();
            $indata['user_id']=$userinfo['id'];
            $indata['jianshebao_id']=$jianshebao_id;
            $indata['amount']=$amount;
            $indata['yugu_amount']=$yugu_amount;
            $indata['all_days']=$jianshebaoinfo['days'];
            $indata['create_time']=time();
            $recordModel->insert($indata);

            Db::commit();
            $this->success('投资成功');
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    public function mytouzilist(){
        $userinfo = $this->auth->getUserinfo();
        $recordModel = new Jianshebaorecord();
        $list = collection($recordModel->where(['user_id'=>$userinfo['id']])->select());
        if($list){
            $list = $list->toArray();
            $newlist=[];
            foreach ($list as $k=>$v){
                $newlist[$k]['id']=$v['id'];
                $newlist[$k]['amount']=$v['amount'];
                $newlist[$k]['yugu_amount']=$v['yugu_amount'];
                $newlist[$k]['send_amount']=$v['send_amount'];
                $newlist[$k]['create_time']=date('Y-m-d H:i:s',$v['create_time']);
                if($v['status']==0){
                    $newlist[$k]['statusname']='进行中';
                }else if($v['status']==1){
                    $newlist[$k]['statusname']='已结束';
                }else if($v['status']==2){
                    $newlist[$k]['statusname']='已取出';
                }
            }
            $this->success('成功',$newlist);
        }else{
            $this->success('成功',[]);
        }
    }

    public function sell(){
        $id = $this->request->param('id');
        $userinfo = $this->auth->getUserinfo();
        $recordModel = new Jianshebaorecord();
        $recordinfo = $recordModel->where(['id'=>$id])->select()->find()->toArray();
        if($recordinfo['user_id']!=$userinfo['id']){
            $this->error('不是自己的投资，不能取出');
        }
        if($recordinfo['status'] != 0){
            $this->error('不是进行中状态的投资，不能取出');
        }
        Db::startTrans();
        try{
            $userModel = new \app\admin\model\User();
            $userModel->where(['id'=>$userinfo['id']])->update([
                'shouyi_balance'=>$userinfo['shouyi_balance']+$recordinfo['amount']
            ]);

            $recordModel->where(['id'=>$id])->update([
               'status'=>2,
               'end_time'=>time()
            ]);
            Db::commit();
            $this->success('投资成功');
        }catch (Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**自动发放建设宝收益**/
    public function sendjianshebao(){
        $jianshebaoModel = new \app\admin\model\jianshebao\Jianshebao();
        $jianshebaorecordModel = new Jianshebaorecord();
        $userModel = new \app\admin\model\User();
        $jianshebaosendModel = new Jianshebaosend();
        $list = collection($jianshebaorecordModel->where(['status'=>0])->select());
        if($list){
            $list = $list->toArray();
            Db::startTrans();
            try{
                foreach ($list as $k=>$v){
                    $jianshebaoinfo = $jianshebaoModel->where(['id'=>$v['jianshebao_id']])->find()->toArray();
                    $one = $v['amount']*$jianshebaoinfo['bili']/100/$jianshebaoinfo['days'];

                    //修改用户建设宝收益
                    $userModel->where(['id'=>$v['user_id']])->setInc('shouyi_balance',$one);
                    $userModel->where(['id'=>$v['user_id']])->setInc('jianshebao_amount',$one);

                    //修改建设宝购买纪录
                    $jianshebaorecordModel->where(['id'=>$v['id']])->setInc('send_amount',$one);
                    $jianshebaorecordModel->where(['id'=>$v['id']])->setInc('send_days',$one);

                    $recordinfo = $jianshebaorecordModel->where(['id'=>$v['id']])->find()->toArray();
                    if($recordinfo['all_days'] == $recordinfo['send_days']){
                        $jianshebaorecordModel->where(['id'=>$v['id']])->update(['status'=>1]);
                    }

                    //写入一条资金发送纪录
                    $indata['user_id'] = $v['user_id'];
                    $indata['all_amount'] = $v['amount'];
                    $indata['amount'] = $one;
                    $indata['bili'] = $jianshebaoinfo['bili'];
                    $indata['create_time'] = time();
                    $jianshebaosendModel->insert($indata);
                }
                Db::commit();
                $this->success('发放建设宝收益成功1');
            }catch(Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
        }else{
            $this->success('发放建设宝收益成功');
        }
    }
}